package BlogMybatis.blog.controller;


import BlogMybatis.blog.pojo.Blog;
import BlogMybatis.blog.pojo.Tag;
import BlogMybatis.blog.pojo.Type;
import BlogMybatis.blog.service.BlogService;
import BlogMybatis.blog.service.TagService;
import BlogMybatis.blog.service.TypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class IndexController {
    
    @Autowired
    private BlogService blogService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private TagService tagService;

    //博客首页
    @GetMapping("/")
    public String index(@RequestParam(required = false,defaultValue = "1") int pageNum, Model model){
        PageHelper.startPage(pageNum,8);
        //首页博客数量
        List<Blog> indexBlog = blogService.getIndexBlog();
        //首页右侧分类数量
        List<Type> indexType = typeService.getIndexType();
        //首页右侧标签数量
        List<Tag> indexTag = tagService.getIndexTag();
        //首页右侧推荐数量
        List<Blog> allRecommendBlog = blogService.getAllRecommendBlog();
        PageInfo pageInfo = new PageInfo(indexBlog);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("types",indexType);
        model.addAttribute("tags",indexTag);
        model.addAttribute("recommendBlogs",allRecommendBlog);
        return "index";
    }

    //退出登录跳转到首页
    @GetMapping("/index")
    public String index1(@RequestParam(required = false,defaultValue = "1") int pageNum, Model model,
                         @RequestHeader(value = "User-Agent",required=true) String userAgent){
        PageHelper.startPage(pageNum,8);
        //首页博客数量
        List<Blog> indexBlog = blogService.getIndexBlog();
        //首页右侧分类数量
        List<Type> indexType = typeService.getIndexType();
        //首页右侧标签数量
        List<Tag> indexTag = tagService.getIndexTag();
        //首页右侧推荐数量
        List<Blog> allRecommendBlog = blogService.getAllRecommendBlog();
        PageInfo pageInfo = new PageInfo(indexBlog);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("types",indexType);
        model.addAttribute("tags",indexTag);
        model.addAttribute("recommendBlogs",allRecommendBlog);
        return "index";
    }

    //全局搜索
    @RequestMapping("/search")
    public String search(@RequestParam(required = false,defaultValue = "1") int pageNum,
                         @RequestParam String query,Model model){
        PageHelper.startPage(pageNum,5);
        List<Blog> searchBlog = blogService.getSearchBlog("%"+query+"%");
        PageInfo pageInfo = new PageInfo(searchBlog);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("query",query);
        return "search";
    }

    //博客详情信息
    @GetMapping("/blog/{id}")
    public String blogDetail(@PathVariable("id") Long id,Blog blog,Model model){
        blog.setViews((long) blogService.updateViews(id));
        Blog detailedBlog = blogService.getDetailedBlog(id);
        model.addAttribute("blog",detailedBlog);
        return "blog";
    }

    //底部跳转到最新发布博客
    @GetMapping("/footer/newblog")
    public String newblogs(Model model){
        List<Blog> blogFooter = blogService.listBlogFooter();
        model.addAttribute("newblogs",blogFooter);
        return"fragments :: newBlogList";
    }
}
